const process = require('process')
const puppeteer = require('puppeteer')
const {extractMonth} = require('./utils.js')
const data = require('./data.json')
const {username, password} = require('./config.json')
const siteurl = 'http://jxzlxt.jhc.cn/'

async function run() {
    const browser = await puppeteer.launch({
        //headless: false
    })
    const page = await browser.newPage()
    await page.setViewport({
        width: 1024, height: 768, deviceScaleFactor: 1
    })
    await page.setJavaScriptEnabled(true)
    await page.goto(siteurl)
    page.on('response', resp => {
        console.log(resp.url())
    })
    await login(page)
    await page.waitForResponse(resp => {
        return resp.url() === 'https://jxzlxt.jhc.cn/xtgl/menu_index.html' && resp.status() === 200
    })
    await page.waitForTimeout(2000)
    //await page.screenshot({path: 'first.png'})
    await page.evaluate((username, password) => {
        document.querySelector('#main-nav > li:nth-child(2) > ul:nth-child(2) > li:nth-child(4) > ul:nth-child(2) > li:nth-child(1) > a:nth-child(1)').click()
    }, username, password)
//    await page.waitForResponse(resp => {
//        return resp.url().startsWith('https://jxzlxt.jhc.cn/query/queryCenterManager_ajaxIndex2.html') && resp.status() === 200
//    })
    while (true) {
        await dofill(page)
    }
}

async function login(page) {
    await page.evaluate((username, password) => {
        document.getElementById('yhm').value = username
        document.getElementById('mm').value = password
        document.getElementById('btn_dl_c').click()
    }, username, password)
}

async function dofill(page) {
    await page.waitForSelector('#jqg_tabGrid_0_1')
    //await page.screenshot({path: 'select_log.png'})
    let trLen = await page.evaluate(() => {
        return document.querySelectorAll('#tabGrid_0 tr').length
    })
    if (!trLen || trLen === 0) {
        console.log('no entry left. exit.')
        page.close()
        process.exit(0)
    }
    await page.evaluate(() => {
        document.getElementById('jqg_tabGrid_0_1').click()
    })
    await page.waitForTimeout(500)
    await page.evaluate(() => {
        document.getElementById('btn_txjxrz').click()
    })
    await page.waitForTimeout(500)
    await page.evaluate(() => {
        document.querySelector('button.btn:nth-child(1)').click()
    })
    //await page.waitForTimeout(2000)
    //await page.screenshot({path: 'select_log.png'})
    await page.waitForFunction(() => {
        let ele = document.querySelector('#rows_5_1_td > div:nth-child(1) > label:nth-child(3)')
        return ele && ele.textContent && ele.textContent.length > 0
    })
    let thedate = await page.evaluate(() => {
        return document.querySelector('#rows_5_1_td > div:nth-child(1) > label:nth-child(3)').textContent.trim()
    })
    //console.log('thedate=' + thedate)
    let course_name = await page.evaluate(() => {
        return document.querySelector('#rows_1_1_td > div:nth-child(1) > label:nth-child(3)').textContent.trim()
    })
    console.log(course_name)
    let themonth = extractMonth(thedate)
    let content = data[course_name][themonth]
    //console.log(content)
    await page.evaluate(content => {
        let area = document.getElementById('SKNR_3')
        area.value = content
        document.querySelector('.form-actions > button:nth-child(1)').click()
    }, content)
    await page.waitForTimeout(200)
    await page.evaluate(() => {
        document.querySelector('button.btn:nth-child(2)').click()
    })
    await page.waitForTimeout(500)
    //await page.screenshot({path: 'filled.png'})
}

run().catch(err => {
    console.log(err)
    process.exit(1)
})
